import requests
from lxml import etree
from bs4 import BeautifulSoup
def downloadHtml(url):
    print(url)
    header = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
    try:
        res = requests.get(url,headers=header)
        if res.status_code==200:
            return res.text
        else:
            res.raise_for_status()
    except:
        print('downloadHtml:'+ url +" error!")
        return 'ERROR'
def parserHtmlByCss(html):
    ehtml = etree.HTML(html)
    for i in ehtml.cssselect('.hd>a>span:nth-child(1)'):
        print(i.text)
    for i in ehtml.cssselect('.bd>p'):
        print(i.text)
    for i in ehtml.cssselect('.star>span.rating_num'):
        print(i.text)
    for i in ehtml.cssselect('.star>span:nth-child(4)'):
        print(i.text)
    for i in ehtml.cssselect('.quote>span'):
        print(i.text)
    for i in ehtml.cssselect('.pic>a>img'):
        print(i.attrib['src'])
def parserHtmlByBs4(html):
    soup = BeautifulSoup(html,'html.parser')
    temp = soup.find_all(class_="pic")
    for i in temp:
        print(i.a.img['src'])
        print(i.a.img['alt'])
    temp = soup.find_all(class_="bd")
    for i in temp:
        typetype = i.p
        print(str(typetype)[140:150])
    temp = soup.find_all(class_="star")
    for i in temp:
        print(i.contents[3].string)
        print(i.contents[7].string)
    temp = soup.find_all(class_="inq")
    for i in temp:
        print(i.string)
def parserHtmlByXpath(html):
    ehtml = etree.HTML(html)
    xtitle = ehtml.xpath("//div[@class='hd']/a/span[1]/text()")
    print(xtitle)
    xtitle = ehtml.xpath("//div[@class='bd']/p[1]/text()[2]")
    for i in xtitle:
        print(i[41:50])
    xtitle = ehtml.xpath("//div[@class='star']/span[2]/text()")
    print(xtitle)
    xtitle = ehtml.xpath("//div[@class='star']/span[4]/text()")
    print(xtitle)
    xtitle = ehtml.xpath("//span[@class='inq']/text()")
    print(xtitle)
    xtitle = ehtml.xpath("//div[@class='pic']/a//@src")
    print(xtitle)
if __name__=='__main__':

    for i in range(0,10):
        url = 'https://movie.douban.com/top250?start='+ str(i*25)
        print(url)
        strhtml = downloadHtml(url)
        movieInfo1 = parserHtmlByCss(strhtml)
        movieInfo2 = parserHtmlByBs4(strhtml)
        movieInfo3 = parserHtmlByXpath(strhtml)
